package org.example.traffic.student;

import org.example.traffic.student.Student;
import org.example.traffic.student.StudentRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StudentService {
    private final StudentRepository studentRepository;

    public StudentService(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }


    public Student saveStudent(Student student) {
        return studentRepository.save(student);
    }

    public Student getStudentById(Integer id) {
        return studentRepository.findById(id).orElse(null);
    }

    public List<Student> getAllStudents() {
        return studentRepository.findAll();
    }


    public void deleteStudentById(Integer id) {
        studentRepository.deleteById(id);
    }


    public Student updateStudent(Student student) {
        return studentRepository.save(student);
    }


    public List<Student> findStudentsByName(String name) {
        return studentRepository.findByNameContaining(name);
    }

    public List<Student> findStudentsByOrganizationId(Integer organizationId) {
        return studentRepository.findByOrganizationId(organizationId);
    }


}
